import 'dart:convert';

import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:zootix/models/zt_park_detail_comment.model.dart';
import 'package:zootix/services/zt_storage.service.dart';
import 'package:zootix/services/zt_translate.service.dart';
import 'package:zootix/util/zt_convert.dart';

import '../../../constants/zt_constants.dart';
import '../../../util/zt_util.dart';

class ZtParkDetailController extends GetxController {
  final swipeController = SwiperController();
  late String parkType = '';
  final selectedTabIndex = 0.obs;
  final textEditingController = TextEditingController();
  final comments = RxList<ZtParkDetailCommentModel>([]);
  final isTranslating = RxBool(false);
  final translatedText = RxString('');
  Rx<Map<String, dynamic>?> parkDetail = Rx<Map<String, dynamic>?>(null);
  ZtParkDetailController();

  @override
  onReady() {
    parkDetail.value = ZtData.parkDetail[parkType] as Map<String, dynamic>;
    getComments();
    super.onReady();
  }

  Future<List<ZtParkDetailCommentModel>> getComments() async {
    String? data = ZtStorageService.to.getString(ZtCacheKeys.parkComments);
    List<ZtParkDetailCommentModel> result = [];
    if (data != null) {
      result = toList(jsonDecode(data))
          .map((e) => ZtParkDetailCommentModel.fromJson(e))
          .toList();
    }
    comments.assignAll(
        result.where((element) => element.parkType == parkType).toList());
    return result;
  }

  Future<bool> addComments(ZtParkDetailCommentModel comment) async {
    ZtLogManger().putLog(
      ZtLogType.clickEvent,
      ZtLogManger.buildEvent(
          logType: ZtLogType.clickEvent, page: 'addComments'),
    );
    List<ZtParkDetailCommentModel> result = await getComments();
    bool res = await ZtStorageService.to.setString(
        ZtCacheKeys.parkComments,
        jsonEncode([
          comment,
          ...result,
        ]));
    return res;
  }

  Future<bool> likeComment(String id) async {
    List<ZtParkDetailCommentModel> result = await getComments();
    result = result.map((e) {
      if (e.id == id) {
        e.isLike = !e.isLike;
      }
      return e;
    }).toList();
    bool res = await ZtStorageService.to
        .setString(ZtCacheKeys.parkComments, jsonEncode(result));
    getComments();
    return res;
  }

  Future<void> translateMessage() async {
    isTranslating.value = true;
    int translateNum = 2;
    await ZtTranslateService.to.googleTranslate(parkDetail.value!['title'],
        (value) {
      parkDetail.value = {...parkDetail.value!, 'title': value};
      translateNum--;
      if (translateNum == 0) {
        isTranslating.value = false;
        translatedText.value = value;
      }
    });
    await ZtTranslateService.to.googleTranslate(parkDetail.value!['content'],
        (value) {
      parkDetail.value = {...parkDetail.value!, 'content': value};
      translateNum--;
      if (translateNum == 0) {
        isTranslating.value = false;
        translatedText.value = value;
      }
    });
  }
}
